// 大数据-mysql服务处理

// 引入数据库模块
const db = require('../config/mysql.js')
const utils = require("../common/utils")
const bcrypt = require('bcryptjs')


// 获取农作物产量-折线图
const getCrop = () => {
  return new Promise((resolve, reject) => {
    let sql = `select * from bigData where type = 1`
    db.query(sql, (err, result) => {
      if (err) return reject(err)
      let legend = []
      let nf = []
      let cl = []
      let mj = []
      result.forEach(item => {
        if (legend.indexOf(item.name) == -1) legend.unshift(item.name)
        // 年份-x轴
        if (nf.indexOf(item.year) == -1) nf.unshift(item.year)
        // 产量
        if (item.num) cl.unshift(item.num)
        // 面积
        if (item.area) mj.unshift(item.area)
      })
      return resolve({
        legend, nf, mj, cl
      })
    })
  })
}

// 获取农产品进出口金额-柱状图
const getBar = () => {
  return new Promise((resolve, reject) => {
    let sql = `select * from bigData where type = 2`
    db.query(sql, (err, result) => {
      if (err) return reject(err)
      let legend = []
      let yf = []
      let ck = []
      let jk = []
      result.forEach(item => {
        if (legend.indexOf(item.name) == -1) legend.unshift(item.name)
        // 月份-x轴
        if (yf.indexOf(item.month) == -1) yf.unshift(item.month)
        if (item.isImport == 1) {
          // 进口
          if (item.price) jk.unshift(item.price)
        } else {
          // 出口
          if (item.price) ck.unshift(item.price)
        }
      })
      return resolve({
        legend, yf, jk, ck
      })
    })
  })
}

// 获取农产品进出口主要地区/金额-饼图/雷达
const getPie = (type) => {
  return new Promise((resolve, reject) => {
    let sql = `select * from bigData where type = 3 and isImport = ${type}`
    db.query(sql, (err, result) => {
      if (err) return reject(err)
      let pieData = []
      let ldData = []
      result.forEach(item => {
        pieData.push({
          name: item.name,
          value: item.price
        })
        ldData.push(item.price)
      })
      return resolve({
        pieData, ldData
      })
    })
  })
}

// 获取农产品数据表格
const getTable = (type) => {
  return new Promise((resolve, reject) => {
    let sql = `select * from bigData where type = 4`
    db.query(sql, (err, result) => {
      if (err) return reject(err)
      return resolve(result)
    })
  })
}

module.exports = {
 getCrop,
 getBar,
 getPie,
 getTable
}